using System.Collections.Generic;
using UnityEngine;

public class PromoRewardCotrollerUI : MonoBehaviour
{
	[SerializeField]
	private GameObject holder;

	[SerializeField]
	private UIGrid rewardGrid;

	[SerializeField]
	private UIScrollView rewardScroll;

	[SerializeField]
	private GameObject arrows;

	[SerializeField]
	private PromoRewardUI promoReward;

	[SerializeField]
	private GameObject backGround;

	private List<PromoRewardUI> promoRewardList = new List<PromoRewardUI>();

	private void Awake()
	{
		promoReward.gameObject.SetActive(false);
	}

	public Transform SetRewardUI(string id, int amount)
	{
		holder.gameObject.SetActive(true);
		if (!backGround.activeInHierarchy)
		{
			backGround.SetActive(true);
		}
		PromoRewardUI promoRewardUI = Object.Instantiate(promoReward);
		promoRewardUI.transform.SetParent(rewardGrid.transform);
		promoRewardUI.transform.localPosition = Vector3.zero;
		promoRewardUI.transform.localScale = Vector3.one;
		promoRewardList.Add(promoRewardUI);
		if (promoRewardList.Count > 4)
		{
			arrows.SetActive(true);
			rewardScroll.contentPivot = UIWidget.Pivot.TopLeft;
			rewardGrid.pivot = UIWidget.Pivot.Left;
		}
		else
		{
			arrows.SetActive(false);
			rewardScroll.contentPivot = UIWidget.Pivot.Center;
			rewardGrid.pivot = UIWidget.Pivot.Center;
		}
		promoRewardUI.SetUI(id, amount);
		rewardScroll.ResetPosition();
		return promoRewardUI.transform;
	}

	public void ClearRewards()
	{
		for (int i = 0; i < promoRewardList.Count; i++)
		{
			Object.Destroy(promoRewardList[i].gameObject);
		}
		promoRewardList.Clear();
	}

	public void HidePromoRewardUI()
	{
		ClearRewards();
		holder.gameObject.SetActive(false);
	}
}
